package com.ssu.sslab.typeConvertor;


/**
 * Type Change.
 * 
 */
public class TypeConvertor {
	
	
	/**
	 * 32 int to 4 bytes array. 
	 * @param buf
	 * @param begin 
	 */
	public static void Int2Byte(int data, byte[] buf, int begin){
	
		for (int i = 0; i < 4; i++) {
			buf[begin+i] = (byte) ((data >> 8 * (3 - i)) & 0xff); 
		}
	}
	
	
	/**
	 * 4 bytes array to 32 int.
	 * @param b
	 * @param begin
	 * @return value
	 */
	public static int Byte2Int(byte[] b,int begin){
		 
		int value = 0;
		for (int i = 0; i < 4; i++) {
			value += (b[begin+i] & 0xff) << (8 * (3-i));
		}
		return value;
	}
	
	/**
	 * 16 short to 2 bytes array.
	 * @param data
	 * @param buf
	 * @param begin
	 */
	public static void Short2Byte(short data, byte[] buf, int begin){
		for (int i = 0; i < 2; i++) {
			int offset = (1-i) * 8;
			buf[begin+i] = (byte)((data >>> offset) & 0xff);
		}
	}
	
	/**
	 * 2 bytes array to short(or 16 int).
	 * @param buf
	 * @param begin
	 * @return target
	 */
	
	public static short Byte2Short(byte[] buf, int begin){
		int target = (buf[begin+1] & 0xff) | ((buf[begin] << 8) & 0xff00);
		return (short)target;
		
	}
	
	
	
	/************************************************************************************
	* In the following, byte, short, int, double,long,float, string to byte array.
	*************************************************************************************/
	
	
	/*
	 * byte to byte array.
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, byte data ) {
		for( int i = (Byte.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i--, start++ )  {
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				arr[start] |= (data >> ( j + ( i * Byte.SIZE ) ) & 1) << j;				
			}
		}
	}
	// boolean to byte array.
	public static void SetValueToByteArray( byte [] arr, int  start, Boolean data ) {
		if (data.booleanValue() == true) {
			arr[start] = (byte)1;
		}else {
			arr[start] = (byte)0;
		}
	}
	
	
	/*
	 * string to byte array.
	 */
	public static int SetValueToByteArray(byte [] arr, int start, String string) {
		
		byte[] buf = string.getBytes();
		for (int i = 0; i < buf.length; i++) {
			arr[start+i] = buf[i];
		}
		return buf.length;
	}
	
	/*
	 * short to byte array
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, short data ) {
		for( int i = (Short.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i--, start++ )  {
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				arr[start] |= (data >> ( j + ( i * Byte.SIZE ) ) & 1) << j;				
			}
		}
	}
	
	/*
	 * int to byte array.
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, int data ) {
		for( int i = (Integer.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i--, start++ )  {
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				arr[start] |= (data >> ( j + ( i * Byte.SIZE ) ) & 1) << j;				
			}
		}
	}
	
	/*
	 * long to byte array.
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, long data ) {
		for( int i = (Long.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i--, start++ )  {
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				arr[start] |= (data >> ( j + ( i * Byte.SIZE ) ) & 1) << j;				
			}
		}
	}
	
	/*
	 * float to byte array.
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, float data ) {
		int value = Float.floatToIntBits(data);
		
		for( int i = 0 ; i <= (Integer.SIZE / Byte.SIZE) - 1 ; i++, start++ )  {
			
			arr[start] = (byte)(value >> (i * Byte.SIZE) & 0xff);			
		}
	}
	
	/*
	 * double to byte array.
	 */
	public static void SetValueToByteArray( byte [] arr, int  start, double data ) {
		long value = Double.doubleToLongBits(data);
	
		for( int i = 0 ; i <= (Long.SIZE / Byte.SIZE) - 1 ; i++, start++ )  {
			
			arr[start] = (byte)(value >> (i * Byte.SIZE) & 0xff);			
		}
	}
	
	
	/******************************************************************************************
	*		In the following, convert byte array to byte,int,long,double,short, float,string.
	*******************************************************************************************/
	
	/*
	 * byte array to byte from start.
	 */
	public static byte SetByteArrayToByte( byte [] arr, int  start ) {
		byte temp = 0;
		
		for( int i = (Byte.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i-- )  {
			byte b = arr[start++];			
			
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				temp |= (b >> j & 1) << ( j + ( i * Byte.SIZE ) );				
			}
		}
		
		return temp;	
	}
	
	/*
	 * byte array to boolean from start
	 */
	public static boolean SetByteArrayToBoolean(byte [] arr, int start) {
		if (arr[start] == (byte)1) {
			return true;
		}else {
			return false;
		}
	}
	
	/*
	 * byte array to string from start
	 */
	public static String SetByteArrayToString(byte[] arr, int start, int length) {
		String res = new String(arr, start, length);
		return res;
	}
	
	
	/*
	 * byte array to short from start.
	 */
	public static short SetByteArrayToShort( byte [] arr, int  start ) {		
		short temp = 0;
		
		for( int i = (Short.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i-- )  {
			byte b = arr[start++];			
			
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				temp |= (b >> j & 1) << ( j + ( i * Byte.SIZE ) );				
			}
		}
		
		return temp;
	}
	
	/*
	 * byte array to int from start
	 */
	public static int SetByteArrayToInt( byte [] arr, int  start ) {		
		int temp = 0;
		
		for( int i = (Integer.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i-- )  {
			byte b = arr[start++];			
			
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				temp |= (b >> j & 1) << ( j + ( i * Byte.SIZE ) );				
			}
		}
		
		return temp;
	}
	
	/*
	 * byte array to long from start
	 */
	public static long SetByteArrayToLong( byte [] arr, int  start ) {		
		long temp = 0;
		
		for( int i = (Long.SIZE / Byte.SIZE) - 1 ; i >= 0 ; i-- )  {
			byte b = arr[start++];			
			
			for( int j = Byte.SIZE - 1 ; j >= 0 ; j-- ) {				
				temp |= (b >> j & 1) << ( j + ( i * Byte.SIZE ) );
			}
		}
		
		return temp;
	}
	
	/*
	 * byte array to float from start.
	 */
	public static float SetByteArrayToFloat( byte [] arr, int  start ) {		
		int value = 0;
        
        for ( int shiftBy = 0; shiftBy < Integer.SIZE; shiftBy += 8 ) {
        	value |= ( (int)( arr[start++] & 0xff ) ) << shiftBy;
        }
        
        return Float.intBitsToFloat(value);
	}
	
	/*
	 * byte array to double from start.
	 */
	public static double SetByteArrayToDouble( byte [] arr, int  start ) {		
		long value = 0;
        
        for ( int shiftBy = 0; shiftBy < Long.SIZE; shiftBy += 8 ) {
        	value |= ( (long)( arr[start++] & 0xff ) ) << shiftBy;
        }
        
        return Double.longBitsToDouble(value);
	}	

}
